<template>
  <div>
    <el-dialog
        :model-value="trafficDrawer"
        :title="title"
        class="traffic-replay-dialog"
        @open="getTrafficList"
        @close="closeFunc"
        destroy-on-close
    >
      <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          class="mx-2 mt-1"
          label-position="right"
      >
        <el-form-item prop="ID">
          <el-select
              v-model="form.ID"
              placeholder="选择流量包"
              @change="changeItem"
              clearable
          >
            <el-option
                v-for="item in trafficList"
                :key="item.id"
                :label="item.name + '-' + item.size"
                :value="item.ID"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div style="margin-top: 10px; text-align: center">
        <el-button @click="closeFunc">取消</el-button>
        <el-button type="primary" @click="trafficReplayFunc">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { getResourceLibraryTrafficList } from '@/api/topo/trafficSimulation';
import { trafficReplay as trafficReplayApi } from '@/api/topo/component';
const formRef = ref(null);
const emit = defineEmits(['closeDialog']);
const props = defineProps({
  trafficDrawer: {
    default: function () {
      return false;
    },
    type: Boolean,
  },
  info: {
    default: function () {
      return {};
    },
    type: Object,
  },
  id: {
    default: function () {
      return '';
    },
    type: String,
  },
  cid: {
    default: function () {
      return '';
    },
    type: String,
  },
  clusId: {
    default: function () {
      return null;
    },
    type: Number,
  },
});

//详情修改
const form = ref({
  baseImagePath: '',
  type: null,
  username: '',
  password: '',
  ipAddr: '',
  port: '',
  ID: '',
});
const title = ref('选择资源库流量包');
const trafficList = ref([]);
const rules = {
  ID: [{ required: true, trigger: 'change', message: '不能为空' }],
};
const getTrafficList = () => {
  getResourceLibraryTrafficList({ page: 1, pageSize: 9999 }).then(res => {
    trafficList.value = res.data?.list;
  });
};

const trafficReplayFunc = () => {
  formRef.value?.validate(valid => {
    if (valid) {
      trafficReplayApi(props.id, props.info.id, form.value).then(res => {
        if (!(res instanceof Error)) {
          ElMessage.success(res.msg);
        }
        closeFunc();
      });
    }
  });
};

const changeItem = res => {
  trafficList.value.forEach(item => {
    if (item.ID === res) {
      form.value.name = item.name;
      form.value.type = item.equipment?.type;
      form.value.username = item.equipment?.userName;
      form.value.password = item.equipment?.passWord;
      form.value.baseImagePath = item.path;
      form.value.ipAddr = item.equipment?.ipAddr;
      form.value.port = item.equipment?.port;
      form.ID = item.ID;
    }
  });
};

const closeFunc = () => {
  formRef.value.resetFields();
  emit('closeDialog');
};
</script>

<style lang="scss">
.net_tab {
  .el-form-item__label {
    word-break: keep-all;
  }
  .el-descriptions {
    border: 1px solid #ccc;
    padding: 10px;
    border-radius: 5px;
    box-sizing: border-box;
    margin-top: 5px;
    .del_icon {
      font-size: 20px;
      cursor: pointer;
    }
  }
  .flexBetween {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.drawer {
  position: absolute;
  top: 50%;
  transition: all 0.5s;
  width: 400px;
  //padding: 10px;
  right: 0px;
  transform: translateY(-50%);
  height: 83.5%;
  background: #0b3367;
  border-radius: 5px;
  box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
  box-sizing: border-box;
  z-index: 9999;
  color: #fff;
  .color_red {
    color: #f56c6c;
  }
  .avatar-uploader {
    padding: 10px 20px;
    background-color: #002554;
    border: 1px solid #1158a8;
    border-radius: 4px;
  }
  .avatar {
    width: 100px;
    height: 100px;
  }
  .el-tabs__content {
    overflow-y: auto !important;
    overflow-x: auto !important;
  }
  .moduleConfigTitle {
    border-bottom: 1px solid #1158a8;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
  }
  .basicConfig {
    height: 100%;
    //overflow: scroll;
    .basicConfig_title {
      // border-left: 5px solid #0e4eee;
      //padding-left: 0px;
    }
  }
  .flexStart {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding: 7px 0;
    box-sizing: border-box;
    .data {
      width: 70%;
      text-align: right;
      cursor: pointer;
    }
    .title {
      margin-left: 5px;
    }
    .titleText {
      width: 30%;
      text-align: right;
    }
  }
  .pointer {
    cursor: pointer;
  }
  .network,
  .cpu,
  .memory {
    padding: 0 20px;
    height: 40px;
    line-height: 40px;
    margin-top: 10px;
    background-color: #002554;
  }
  .list {
    margin-top: 5px;
    padding: 5px;
  }
  .disabledList {
    border: 1px dashed #f56c6c;
    cursor: not-allowed;
  }
}
</style>
<style lang="scss" scoped>
:deep(.traffic-replay-dialog) {
  width: 560px;
  .el-form {
    max-width: 460px;
  }
}
</style>
